package com.irdstudio.efp.riskm.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.riskm.service.dao.CooprOrgInfoDao;
import com.irdstudio.efp.riskm.service.domain.CooprOrgInfo;
import com.irdstudio.efp.riskm.service.facade.CooprOrgInfoService;
import com.irdstudio.efp.riskm.service.vo.CooprOrgInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 合作机构信息表				<p>
 * @author ligm
 * @date 2019-08-10
 */
@Service("cooprOrgInfoService")
public class CooprOrgInfoServiceImpl implements CooprOrgInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(CooprOrgInfoServiceImpl.class);

	@Autowired
	private CooprOrgInfoDao cooprOrgInfoDao;
	
	/**
	 * 新增操作
	 */
	public int insertCooprOrgInfo(CooprOrgInfoVO inCooprOrgInfoVo) {
		logger.debug("当前新增数据为:"+ inCooprOrgInfoVo.toString());
		int num = 0;
		try {
			CooprOrgInfo cooprOrgInfo = new CooprOrgInfo();
			beanCopy(inCooprOrgInfoVo, cooprOrgInfo);
			logger.debug("新增的参数为:"+cooprOrgInfo);
			num = cooprOrgInfoDao.insertCooprOrgInfo(cooprOrgInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(CooprOrgInfoVO inCooprOrgInfoVo) {
		logger.debug("当前删除数据条件为:"+ inCooprOrgInfoVo);
		int num = 0;
		try {
			CooprOrgInfo cooprOrgInfo = new CooprOrgInfo();
			beanCopy(inCooprOrgInfoVo, cooprOrgInfo);
			num = cooprOrgInfoDao.deleteByPk(cooprOrgInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCooprOrgInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(CooprOrgInfoVO inCooprOrgInfoVo) {
		logger.debug("当前修改数据为:"+ inCooprOrgInfoVo.toString());
		int num = 0;
		try {
			CooprOrgInfo cooprOrgInfo = new CooprOrgInfo();
			beanCopy(inCooprOrgInfoVo, cooprOrgInfo);
			num = cooprOrgInfoDao.updateByPk(cooprOrgInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCooprOrgInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public CooprOrgInfoVO queryByPk(CooprOrgInfoVO inCooprOrgInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inCooprOrgInfoVo);
		try {
			CooprOrgInfo queryCooprOrgInfo = new CooprOrgInfo();
			beanCopy(inCooprOrgInfoVo, queryCooprOrgInfo);
			CooprOrgInfo queryRslCooprOrgInfo = cooprOrgInfoDao.queryByPk(queryCooprOrgInfo);
			if (Objects.nonNull(queryRslCooprOrgInfo)) {
				CooprOrgInfoVO outCooprOrgInfoVo = beanCopy(queryRslCooprOrgInfo, new CooprOrgInfoVO());
				logger.debug("当前查询结果为:"+ outCooprOrgInfoVo.toString());
				return outCooprOrgInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CooprOrgInfoVO> queryAllOwner(CooprOrgInfoVO cooprOrgInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CooprOrgInfoVO> list = null;
		try {
			List<CooprOrgInfo> cooprOrgInfos = cooprOrgInfoDao.queryAllOwnerByPage(cooprOrgInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ cooprOrgInfos.size());
			pageSet(cooprOrgInfos, cooprOrgInfoVo);
			list = (List<CooprOrgInfoVO>) beansCopy(cooprOrgInfos, CooprOrgInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CooprOrgInfoVO> queryAllCurrOrg(CooprOrgInfoVO cooprOrgInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<CooprOrgInfo> cooprOrgInfos = cooprOrgInfoDao.queryAllCurrOrgByPage(cooprOrgInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+cooprOrgInfos.size());
		List<CooprOrgInfoVO> list = null;
		try {
			pageSet(cooprOrgInfos, cooprOrgInfoVo);
			list = (List<CooprOrgInfoVO>) beansCopy(cooprOrgInfos, CooprOrgInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<CooprOrgInfoVO> queryAllCurrDownOrg(CooprOrgInfoVO cooprOrgInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<CooprOrgInfo> cooprOrgInfos = cooprOrgInfoDao.queryAllCurrDownOrgByPage(cooprOrgInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ cooprOrgInfos.size());
		List<CooprOrgInfoVO> list = null;
		try {
			pageSet(cooprOrgInfos, cooprOrgInfoVo);
			list = (List<CooprOrgInfoVO>) beansCopy(cooprOrgInfos, CooprOrgInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	/**
	 * 
	 * @param cooprOrgInfoVo
	 * @return
	 * @author liuty
	 * @date 2019年8月28日 下午8:03:11
	 * @version 0.1
	 */
	@Override
	public List<CooprOrgInfoVO> queryAllCooprOrgByPage(CooprOrgInfoVO cooprOrgInfoVo) {
		logger.debug("当前查询合作机构及以下数据信息的参数信息为:");
		List<CooprOrgInfo> cooprOrgInfos = cooprOrgInfoDao.queryAllCooprOrgByPage(cooprOrgInfoVo);
		logger.debug("当前查询合作机构及以下数据信息的结果集数量为:"+ cooprOrgInfos.size());
		List<CooprOrgInfoVO> list = null;
		try {
			pageSet(cooprOrgInfos, cooprOrgInfoVo);
			list = (List<CooprOrgInfoVO>) beansCopy(cooprOrgInfos, CooprOrgInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@Override
	public List<CooprOrgInfoVO> getAllCooprOrgs(CooprOrgInfoVO cooprOrgInfoVo) {
		List<CooprOrgInfoVO> result = null;
		CooprOrgInfo cooprOrgInfo = new CooprOrgInfo();
		beanCopy(cooprOrgInfoVo, cooprOrgInfo);
		List<CooprOrgInfo> cooprOrgInfoVos = cooprOrgInfoDao.getAllCooprOrgs(cooprOrgInfo);
		logger.debug("根据参数查询信息的结果集数量为:"+ cooprOrgInfoVos.size());
		try {
			if(Objects.nonNull(cooprOrgInfoVos) && !cooprOrgInfoVos.isEmpty()){
				result = (List<CooprOrgInfoVO>) beansCopy(cooprOrgInfoVos, CooprOrgInfoVO.class);
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return result;
	}

	@Override
	public int inValidCooprOrgOverCooprDt() {
		LocalDate date = LocalDate.now();
		logger.info("======>终止合作日期到期的合作机构信息开始，当前日期:<======"+date);
		int num = 0;
		try {
			num = cooprOrgInfoDao.getInValidCooprOrgOverCooprDt();
			cooprOrgInfoDao.inValidCooprOrgOverCooprDt();
			logger.info("======>终止合作日期到期的合作机构信息结束，终止的合作方条数为:<======"+num);
		} catch (Exception e) {
			num = -1;
			logger.error("终止合作日期到期的合作机构信息发生异常，异常信息为:"+e.getMessage());
		}
		return num;
	}

	@Override
	public List<CooprOrgInfoVO> getAllCooprOrgOverCooprDt() {
		LocalDate date = LocalDate.now();
		logger.info("======>获取合作日期到期的合作机构信息开始，当前日期:<======"+date);
		List<CooprOrgInfoVO> result = new ArrayList<>();
		try {
			List<CooprOrgInfo> cooprOrgInfoList = cooprOrgInfoDao.getAllCooprOrgOverCooprDt();
			logger.info("======>获取合作日期到期的合作机构信息结束，到期的合作方数量为:<======"+result.size());
			if(Objects.nonNull(cooprOrgInfoList) && !cooprOrgInfoList.isEmpty()){
				result = (List<CooprOrgInfoVO>) beansCopy(cooprOrgInfoList, CooprOrgInfoVO.class);
			}
		} catch (Exception e) {
			result = null;
			logger.error("获取合作日期到期的合作机构信息发生异常，异常信息为:"+e.getMessage());
		}
		return result;
	}
}
